﻿package {
	import flash.display.*
	import flash.utils.Dictionary;
	import flash.geom.*
	import Role;
	public class GameScene extends Sprite{
		var sight:Sprite = new Sprite();
		var map = new Sprite();
		var playerLayer:Sprite = new Sprite();
		var extraLayer:Sprite = new Sprite();
		var objList:Dictionary = new Dictionary();
		var Xaixs:Array = [];
		var Yaixs:Array = [];
		var Zaixs:Array = [];
		var pass:Array,road:Object;
		var sightX:Number = 0,sightY:Number = 0,sightWidth:Number,sightHeight:Number;
		var rightBorder:Number,bottomBorder:Number;
		var roadPass:Function;
		function GameScene(w:Number,h:Number){
			var screen:Shape = new Shape();
			screen.graphics.beginFill(0xFFFFFF);
			screen.graphics.drawRect(0,0,w,h);
			screen.graphics.endFill();
			mask=screen;
			addChild(sight);
			sight.addChild(map);
			sight.addChild(playerLayer);
			sight.addChild(extraLayer);
			extraLayer.mouseEnabled = false;
			sightWidth = w;
			sightHeight = h;
		}
		function setMap(GameMap){
			sight.addChild(GameMap);
			sight.swapChildren(GameMap,map);
			sight.removeChild(map);
			map = GameMap;
			rightBorder = map.width - sightWidth;
			bottomBorder = map.height - sightHeight;
		}
		function addPlayer(name = null){
			var player = new rigid();
			if(name)player.name = name;
			playerLayer.addChild(player);
			return player;
		}
		function delPlayer(name){
			var obj = playerLayer.getChildByName(name);
			obj.clearTarget();
			playerLayer.removeChild(obj);
			
		}
		function getPlayer(name){
			return playerLayer.getChildByName(name);
		}
		function removePlayer(obj){
			playerLayer.removeChild(obj);
		}
		function getRoad(role){
			var Px:Number = role.globalX + role.dx,Py:Number = role.globalY + role.dy;
			var tile:Array = pass[int(Py/200)][int(Px/200)];
			var Pr:Number = role.r
			for(var i:int = tile.length - 1;i >= 0;i--){
				if(Pr > MyGeom.DistanceTriangle(Px,Py,tile[i]))return false;
			}
			return true;
		}
		function mapPass(role){
				roadPass(getRoad(role),role);
		}
		function watch(cameraX,cameraY){
			sightX = cameraX - sightWidth/2;
			sightY = cameraY - sightHeight/2;
			if(sightX < 0)sightX = 0;
			if(sightY < 0)sightY = 0;
			if(sightX > rightBorder)sightX = rightBorder;
			if(sightY > bottomBorder)sightY = bottomBorder;
			sight.x = -sightX;
			sight.y = -sightY;
		}
		//遮挡处理
		function sortZ(a,b){
			if(getZ(a) > getZ(b))return 1;
			return -1;
		}			
		function Zaxis(o){
			var k:int;
			var i = playerLayer.getChildIndex(o);
			var oy = getZ(o)
			if(i < playerLayer.numChildren - 1 && oy > getZ(playerLayer.getChildAt(i + 1))){
				playerLayer.swapChildrenAt(i,i+1);
				for(k = i + 2;k < playerLayer.numChildren;k++){
					if(oy < getZ(playerLayer.getChildAt(k)))break;
					playerLayer.swapChildrenAt(k - 1,k);
				}
			}else if(i > 0 && oy < getZ(playerLayer.getChildAt(i - 1))){
				playerLayer.swapChildrenAt(i,i-1);
				for(k = i - 2;k > 0;k--){
					if(oy > getZ(playerLayer.getChildAt(k)))break;
					playerLayer.swapChildrenAt(k + 1,k);
					
				}
			}
		}
		function getZ(o){
			var oy = o.globalY + o.height;
			return oy;
		}
	}
}
class rigid extends Role{
	public var info:Object = {};
	var appear:Boolean = true;
	var r:Number
	function rigid(){
		visible = false;
	}
	public function start(){
		this.figure.x = -width/2
		this.figure.y = -height + width/2
		this.x = this.globalX
		this.y = this.globalY
		//r = (width - 10)*(width - 10)/4
		r = 800
		visible = true;
	}
}